'use strict';
/**
 * Created by FreezeSoul on 2015/9/28.
 */
define([
        'angular',
        'adConfig',
        'app/modules/dictionary/define',
        'app/modules/dictionary/directives/code-to-name',
        'app/modules/dictionary/services/dictionary',
        'app/modules/dictionary/controllers/dictionary-edit',
        'app/modules/dictionary/controllers/dictionary-delete'
    ],
    function (angular, adConfig) {
        angular.module('ad.dictionary')
            .controller('adDictionaryController', ['$scope', '$uibModal', '_', 'Notification', 'adDictionaryService', function ($scope, $uibModal, _, Notification, service) {
                $scope.dictionaryList = [];
                $scope.isLoading = false;
                $scope.searchWord = "";

                $scope.maxSize = 5;
                $scope.itemsPerPage = adConfig.perPageRowSize;
                $scope.currentPage = 1;
                $scope.totalItems = 0;

                $scope.searchData = function () {
                    $scope.isLoading = true;
                    service.getPage($scope.currentPage, $scope.itemsPerPage, $scope.searchWord).then(function (data) {
                        var obj = angular.fromJson(data);
                        if (obj.result == 'success') {
                            $scope.dictionaryList = obj.data.items;
                            $scope.totalItems = obj.data.count;
                        }
                        $scope.isLoading = false;
                    });
                };

                $scope.pageChanged = function () {
                    $scope.searchData();
                };

                $scope.newData = function () {
                    var modalInstance = $uibModal.open({
                        animation: true,
                        templateUrl: 'app/modules/dictionary/views/dictionary-edit.tpl.html',
                        controller: 'adDictionaryEditController',
                        size: 'lg',
                        resolve: {
                            item: function () {
                                return {dictionary_id: '', dictionary_name: '', dictionary_code: "", dictionary_value: "", dictionary_desc: "", dictionary_type: ""};
                            }
                        }
                    });

                    modalInstance.result.then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                Notification.success('添加成功！');
                            }
                            $scope.searchData();
                        },
                        function () {
                        });
                };

                $scope.editRow = function (row) {
                    var modalInstance = $uibModal.open({
                        animation: true,
                        templateUrl: 'app/modules/dictionary/views/dictionary-edit.tpl.html',
                        controller: 'adDictionaryEditController',
                        size: 'lg',
                        resolve: {
                            item: function () {
                                return row;
                            }
                        }
                    });

                    modalInstance.result.then(function (data) {
                        var obj = angular.fromJson(data);
                        if (obj.result == 'success') {
                            Notification.success('修改成功！');
                        }
                        else if (obj.result == 'repeat') {
                            Notification.error('修改用户账号已经存在！');
                            return false;
                        }
                    }, function () {
                    });
                };

                var deleteItems = function (items) {
                    var modalInstance = $uibModal.open({
                        animation: true,
                        templateUrl: 'app/modules/dictionary/views/dictionary-delete.tpl.html',
                        controller: 'adDictionaryDeleteController',
                        size: 'sm',
                        resolve: {
                            items: function () {
                                return items;
                            }
                        }
                    });

                    modalInstance.result.then(function (data) {
                        var obj = angular.fromJson(data);
                        if (obj.result == 'success') {
                            Notification.success('删除成功！');
                        }

                        $scope.searchData();
                    }, function () {
                    });
                };

                $scope.deleteSelected = function () {
                    var items = _.filter($scope.dictionaryList, function (dictionary) {
                        return dictionary.isSelected;
                    });
                    deleteItems(items);
                };

                $scope.deleteRow = function (row) {
                    deleteItems([row]);
                };

                $scope.searchData();
            }]);
    });